/**
 * @author 陈泰来
 * @version V1.0
 * @Project echo-java-base
 * @Title 委托事件处理类
 * @Description 将观察者模式中的 通知者和观察者完全解耦，使用反射进行方法调用。
 * @Package designmodel.observe2.delegate
 * @date 2021/3/4 1:56 下午
 * @Copyright:学如逆水行舟，不进则退 .
 */
package designmodel.observe2.delegate;

import java.util.List;

public class EventHandler implements IEventHandler {

    List<Event> delegateEvents;

    //委托构造
    public EventHandler(List<Event> observers) {
        this.delegateEvents = observers;
    }


    @Override
    public EventHandler addEvent(Object object, String methodName, Object... methodParams) {
        delegateEvents.add(new Event(object, methodName, methodParams));
        return this;
    }

    @Override
    public EventHandler delEvent(Event e) {
        delegateEvents.remove(e);
        return this;
    }

    @Override
    public void notifyX() throws Exception {
        for (Event event : delegateEvents) {
            event.invoke();
        }
    }


}
